﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PJ.Data;
using PJ.Business;

namespace PJ.Business
{
    public interface IObjectDataContainer
    {
        IObjectData ObjectData { get; set; }
    }
    /// <summary>
    /// DataObject class
    /// </summary>
    /// <typeparam name="OD">Inner data object</typeparam>
    public abstract class BusinessObject<OD> : IBusinessObject, IObjectDataContainer
        where OD : IObjectData
    {
        protected OD m_Data;

        public BusinessObject(OD data)
        {
            if (data == null)
                throw new ArgumentNullException();
            m_Data = data;
        }
        public BusinessObject()
        {
        }
        IObjectData IObjectDataContainer.ObjectData { get { return ObjectData; } set { ObjectData = (OD)value; } }

        public OD ObjectData
        {
            get
            {
                if (m_Data == null)
                    throw new ArgumentNullException();
                return m_Data;
            }
            set { m_Data = value; }
        }

        public string GlobalId
        {
            get
            {

                return ObjectData.GlobalId;
            }
            set
            {
                if (value != ObjectData.GlobalId)
                {
                    ObjectData.GlobalId = value;
                }
            }
        }

        public void InitData(object data)
        {
            if (data == null)
                throw new ArgumentNullException();
            if (data is OD)
            {
                ObjectData = (OD)data;
            }
            else
                throw new InvalidCastException();
        }

        public virtual void CopyFrom(IBusinessObject data)
        {
            if (data == null)
                throw new ArgumentNullException();
            this.GlobalId = data.GlobalId;
        }

        public virtual void Dispose()
        {
            if (m_Data != null)
            {
                m_Data = default(OD);
            }
        }

        public virtual string Error
        {
            get { return null; }
        }

        public virtual string this[string columnName]
        {
            get { return null; }
        }


    }

}

